package list

func detectCycle(head *ListNode) *ListNode {
	len := 0

	nodeMap := map[*ListNode]int{}

	for head != nil {
		if _, ok := nodeMap[head]; ok {
			return head
		}

		nodeMap[head] = len
		head = head.Next
		len++
	}

	return nil
}
